package com.lanlian.cayman.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.bean.MyContractBean;
import com.lanlian.cayman.fragment.MarketFragment;
import com.lanlian.cayman.utils.MResUtil;
import com.lanlian.cayman.utils.NumberManager;
import com.lanlian.commonlib.data.GoodsBean;
import com.lanlian.utillib.ViewFindUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 交易适配器
 */
public class NewMarketAdapter extends BaseAdapter {

    private List<MyContractBean> mList;
    private Context mContext;
    private OnClickListener mOnClickListener;

    public interface OnClickListener {
        void onClick(MyContractBean contractBean, ImageView imageView);
    }

    public NewMarketAdapter(Context context, @Nullable List<MyContractBean> data, OnClickListener onClickListener) {
        this.mContext = context;
        this.mList = data;
        this.mOnClickListener = onClickListener;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_market_item, null);
        }
        LinearLayout mLayPosition = ViewFindUtils.hold(convertView, R.id.lay_position);
        TextView mTvTreaty = ViewFindUtils.hold(convertView, R.id.tv_treaty);
        TextView mTvLast = ViewFindUtils.hold(convertView, R.id.tv_last);
        TextView mTvChange = ViewFindUtils.hold(convertView, R.id.tv_change);
        TextView mTvChg = ViewFindUtils.hold(convertView, R.id.tv_chg);
        LinearLayout mLlIv = ViewFindUtils.hold(convertView, R.id.ll_iv);
        ImageView mIv = ViewFindUtils.hold(convertView, R.id.iv);

        MyContractBean item = mList.get(position);
        if (position % 2 == 0) {
            mLayPosition.setBackgroundColor(mContext.getResources().getColor(R.color.color_bg_text_view_style3));
        } else {
            mLayPosition.setBackgroundColor(mContext.getResources().getColor(R.color.color_bg_text_view_style2));
        }
        mTvTreaty.setText(item.name);

        mLlIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClickListener.onClick(item, mIv);
            }
        });
        GoodsBean goodsBean = MarketFragment.getRespMarket().getGoods(item.id);
        int count = 2;
        if (goodsBean != null) {
            count = NumberManager.getNumberDecimalDigits(goodsBean.minChange);
        }
        if (TextUtils.isEmpty(item.last)) {
            mTvLast.setText("- -");
            mTvChange.setText("- -");
            mTvChg.setText("- -");
            mTvChg.setBackgroundResource(MResUtil.getPingBack());
            mTvChange.setTextColor(MResUtil.getPingColor());
            mTvLast.setTextColor(MResUtil.getPingColor());
        } else {
            mTvLast.setText(NumberManager.getString("1", item.last, count, BigDecimal.ROUND_HALF_UP));
            String chg = NumberManager.getString("1", item.chg, 2, BigDecimal.ROUND_HALF_UP);
            String change = NumberManager.getString("1", item.change, count, BigDecimal.ROUND_HALF_UP);
            mTvChg.setText(chg + "%");
            mTvChange.setText(change);
            if (Double.parseDouble(chg) > 0) {
                mTvChg.setBackgroundResource(MResUtil.getUpBack());
                mTvChange.setTextColor(MResUtil.getUpColor());
                mTvLast.setTextColor(MResUtil.getUpColor());
                if (!change.contains("+")) {
                    mTvChange.setText("+" + change);
                }
                if (!chg.contains("+")) {
                    mTvChg.setText("+" + chg + "%");
                }
            } else if (Double.parseDouble(chg) == 0) {
                mTvChg.setBackgroundResource(MResUtil.getPingBack());
                mTvChange.setTextColor(mContext.getResources().getColor(R.color.color_text_style1));
                mTvLast.setTextColor(mContext.getResources().getColor(R.color.color_text_style1));
            } else {
                mTvChg.setBackgroundResource(MResUtil.getDownBack());
                mTvChange.setTextColor(MResUtil.getDownColor());
                mTvLast.setTextColor(MResUtil.getDownColor());
                if (!change.contains("-")) {
                    mTvChange.setText(change);
                }
                if (!chg.contains("-")) {
                    mTvChg.setText("-" + chg + "%");
                }
            }
        }
        return convertView;
    }

    /**
     * 局部更新数据，调用一次getView()方法；Google推荐的做法
     *
     * @param listView 要更新的listview
     * @param position 要更新的位置
     */
    public void notifyDataSetChanged(ListView listView, int position) {
        /**第一个可见的位置**/
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        /**最后一个可见的位置**/
        int lastVisiblePosition = listView.getLastVisiblePosition();

        /**在看见范围内才更新，不可见的滑动后自动会调用getView方法更新**/
        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
            /**获取指定位置view对象**/
            View view = listView.getChildAt(position - firstVisiblePosition);
            getView(position, view, listView);
        }
    }

}
